<template>
	<div class="homeworkStatus w944">
		<alert ref="alert"></alert>
		<div class="label_top">
			<button @click="reBack"><i><&nbsp;</i>返回</button>
			<p>{{courseName}}</p>
		</div>
		<div class="content">
			<div class="statusType">
				<p><b>作业类型：</b><span>{{homeworkTypeText}}</span></p>
			</div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection"></el-table-column>
				<el-table-column label="序号" width="60">
					<template slot-scope="scope">{{ scope.row.sort}}</template>
				</el-table-column>
				<el-table-column prop="nickname" label="学员名字"></el-table-column>
				<el-table-column label="作业" width="150"> 
					<template slot-scope="scope">
						<el-button @click="handleWatch(scope.row)" v-if="homeworkType=='1-1'">观看</el-button>
						<el-button @click="handleChakan(scope.row)" v-if="homeworkType=='1-2'">查看</el-button>
						<el-button @click="handleDownload(scope.row)" class="red">下载</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="提交时间"></el-table-column>
				<el-table-column prop="homeworkScore" label="评分" width="160">
					<template slot-scope="scope">
						<input type="tel" v-model="scope.row.homeworkScore" class="homeworkScore" @blur="updateScore(scope.row)"/>
					</template>
				</el-table-column>
			</el-table>
			<!--视频文件点击播放-->
			<div class="playerVideo" v-show="playerVideo">
				<a @click="playerClose" class="close">&#10005;</a>
				<video ref='video' controls autoplay>您的浏览器不支持 video元素。</video>
			</div>
			<div class="fenyeMargin">
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import alert from '../../components/Alert.vue';
	export default{
		data(){
			return{
				id:'',
				tableData: [],
				multipleSelection: [],
				sort:'',
				homeworkTypeText:'',
				homeworkScore:'',
				courseid:'',
				courseName:'',
				coursewareId:'', //课程编号id
				homeworkType:'',
				coursewareType:'',//類型
				playerVideo:false,
				videoSrc:'',
				filePreview:false,
				fileUrl:'',
				pagesize: 15,
		        currentpage: 1,
		        total: 1
			}
		},
		methods:{
			getParams() { 
				let that = this;
				this.courseid = this.$route.params.courseid;
				this.coursewareId = this.$route.params.coursewareId;
				this.courseName = this.$route.params.courseName;
				this.homeworkType = this.$route.params.homeworkType;
				this.coursewareType = this.$route.params.coursewareType;
				this.teacherTitle = this.$route.params.teacherTitle;
				this.speakerId = this.$route.params.speakerId;
				if(this.homeworkType == '1-1'){
					this.homeworkTypeText = '视频作业—学员拍摄或上传视频';
				}else if(this.homeworkType == '1-2'){
					this.homeworkTypeText = '文件作业—学员上传文件';
				};
				console.log('teacherhomeworkStatus:',this.$route.params);
			},
			getTableList(){
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/speaker/coursewareHomeworkCommitSpeakerList",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {
						page:this.currentpage,
						num:this.pagesize,
						speakerId:this.courseid,
						coursewareId:this.coursewareId
					},
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
							for(var i = 0;i<this.tableData.length;i++){
								this.tableData[i].sort=i+1
							}
						}
					}
				});
			},
			updateScore(row){
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/coursewareHomeworkCommit/updateHomeworkScore",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'PUT',
					data: {
						id:row.id,
						homeworkScore:row.homeworkScore
					},
					success: res => {
						if(res.data.code == 200) {

						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();	
						}
					}
				});
			},
			handleWatch(row){
				this.playerVideo = true;
				this.$refs.video.src = row.homeworkUrl;
			},
			playerClose(){
				this.playerVideo = false;
				this.$refs.video.src = '';
			},
			handleChakan(row){
				let sUrl = row.homeworkUrl;
				if (!/.(pdf|PDF)$/.test(sUrl)){
					this.fileUrl = "https://view.officeapps.live.com/op/view.aspx?src=" + sUrl
				}else{
					this.fileUrl = sUrl
				};
				window.open(this.fileUrl);
			},
			handleDownload(row){
				let homeworkUrl = row.homeworkUrl;
				if(homeworkUrl!=null && homeworkUrl.indexOf("/") != -1){
					var fileName = homeworkUrl.substring(homeworkUrl.lastIndexOf("/")+1);
				};
				if (window.navigator.msSaveBlob) {
		          	try {
		            	window.navigator.msSaveBlob(blob, fileName)
		          	}catch (e) {
		            	console.log(e);
		          	}
		        }else {
					var downloadElement = document.createElement('a');
		          	downloadElement.href = homeworkUrl;
		          	downloadElement.target = '_blank';
		          	downloadElement.download = fileName; //下载后文件名
		          	document.body.appendChild(downloadElement);
		          	downloadElement.click(); //点击下载
		          	document.body.removeChild(downloadElement); //下载完成移除元素
		          	window.URL.revokeObjectURL(homeworkUrl); //释放掉blob对象
	           	};
				var url = homeworkUrl;
			    var xhr = new XMLHttpRequest();
			    xhr.open('get',url,'jsonp',true); 
			    xhr.responseType = "blob"; // 返回类型blob
			    xhr.onload = function() {
			        if(this.status == 200) {
				        var blob = this.response;
				        var href = window.URL.createObjectURL(blob);
	            	}
	        	};
	    		xhr.send()
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				val.forEach(row => {
					this.multipleSelection.push(row.id);
				});
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			reBack(){
				this.$router.push({
					name:'teacherHomework',
					params:{
						updateTitle:this.teacherTitle,
						speakerId:this.speakerId
					}
				})
			},
		},
		components:{
			alert
		},
		mounted(){
			this.getParams();
			if(this.courseid==null||this.coursewareId==null||this.courseName==null||this.coursewareType==null){
				this.$store.commit('GET_homeworkStatus');
				var s =this.$store.state.homeworkStatus;
				this.courseid = s.courseid,
				this.coursewareId=s.coursewareId,
				this.courseName=s.courseName;
				this.homeworkType = s.homeworkType;
				this.coursewareType=s.coursewareType;
				this.teacherTitle = s.teacherTitle;
				this.speakerId = s.speakerId;
			};
			this.getTableList();
			window.addEventListener("beforeunload",()=>{
				s={
					courseid:this.courseid,
					coursewareId : this.coursewareId,
					courseName:this.courseName,
					homeworkType:this.homeworkType,
					coursewareType:this.coursewareType,
					teacherTitle:this.teacherTitle,
					speakerId:this.speakerId
				}	
				this.$store.commit('SET_homeworkStatus', s);
			});
		}
	}
</script>

<style>
	.homeworkStatus .el-table__row .cell .homeworkScore{width: 60px;height: 28px;border: 1px solid #ccc;border-radius: 3px;color: #333;text-align: center;}
	.homeworkStatus .statusType{margin-bottom: 20px;}
	.homeworkStatus .statusType p{font-size: 14px;color: #333;}
	.homeworkStatus .statusType p b{color: #333;font-size: 15px;}
	.homeworkStatus .playerVideo{width:442px;height:300px;padding:20px 20px 30px;position: fixed;top: 50%;left: 50%;margin-top: -150px;margin-left: -221px;z-index: 999;border:1px solid #ccc;background: #fff;}
	.homeworkStatus .playerVideo .close{color: #333;position: absolute;top: 8px;right: 10px;cursor: pointer;}
	.homeworkStatus .playerVideo video{width:442px;height:250px;object-fit: fill;margin-top: 40px;}
	.homeworkStatus .filePreview{width:800px;height:400px;padding:20px 20px 30px;position: fixed;top: 50%;left: 50%;margin-top: -200px;margin-left: -400px;z-index: 999;border:1px solid #ccc;background: #fff;}
	.homeworkStatus .filePreview .close{color: #333;position: absolute;top: 2px;right: 5px;cursor: pointer;}
	.homeworkStatus .filePreview iframe{width:800px;height:400px;border: 1px solid #ccc;box-sizing: border-box;}
</style>